using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RewardCardUI : UIBase
{
    private Transform cardsContainer;

    private List<int> ranCardList;

    private Button button;

    private void Start()
    {

        ranCardList = new List<int>();

        cardsContainer = transform.Find("CardsContainer");

        button = transform.Find("Button").GetComponent<Button>();

        button.onClick.AddListener(Back);

        FightCardManager.Instance.onClicked += ChooseDone;

        for (int i = 0; i < 3; i++)
        {
            int ran;

            do
            {
                ran = Random.Range(1002, 1009);
            } while (IsSame(ran));

            ranCardList.Add(ran);
            GameObject obj = Instantiate(Resources.Load("UI/CardItem"), transform) as GameObject;
            string cardId = ran.ToString();
            Dictionary<string, string> data = GameConfigManager.Instance.GetCardById(cardId);
            ChooseCard item = obj.AddComponent<ChooseCard>();
            item.Init(data);
            obj.transform.SetParent(cardsContainer);
        }
    }

    private void ChooseDone()
    {
        FightCardManager.Instance.onClicked -= ChooseDone;
        Close();
        UIManager.Instance.ShowUI<SmallWinUI>("SmallWinUI", "UI");
    }

    private void Back()
    {
        Hide();
        UIManager.Instance.ShowUI<SmallWinUI>("SmallWinUI", "UI");
    }

    private bool IsSame(int ran)
    {
        foreach (var ranId in ranCardList)
        {
            if (ran == ranId)
            {
                return true;
            }
        }
        return false;
    }
}
